import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-07-27
 * Time: 13:42
 */
public class SeqList implements IList{
    private int[] array;
    private int size;
    public static final int DEFAULT_CAPACITY = 10;

    public SeqList() {
        this.array = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        if(isFull()) {
            grow();
        }
        this.array[this.size] = data;
        this.size++;
    }
    //对数组进行扩容
    private void grow() {
        this.array = Arrays.copyOf(this.array,
                2*this.array.length);
    }
    //判断数组是否满了
    private boolean isFull() {
        return this.size == array.length;
    }
    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
            if(isFull()) {
                grow();
            }
            for (int i = this.size - 1; i >= pos; i--) {
                this.array[i+1] = array[i];
            }
            this.array[pos] = data;
            this.size++;
        }catch (PosIllegal e){
            System.out.println("插入元素pos位置不合法！！");
            e.printStackTrace();
        }

    }
    //判断pos位置是否合法
    private void checkPos(int pos) {
        if(pos < 0||pos > this.size) {
            throw new PosIllegal("pos位置不合法");
        }
    }

    @Override
    public boolean contains(int toFind) {
        boolean x = false;
        for (int i = 0; i < size; i++) {
            if(array[i] == toFind) {
                x = true;
                break;
            }
        }
        return x;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < size; i++) {
            if(array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        try{
            checkEmpty();
            checkPos1(pos);
            return array[pos];
        }catch (PosIllegal e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
        return -1;
    }
    //判断更改指定位置的pos是否合法
    private void checkPos1(int pos) {
        if(pos < 0 || pos >= this.size) {
            throw new PosIllegal("pos位置不合法");
        }
    }
    //判断顺序表是否为空
    private void checkEmpty() {
        if(isEmpty()) {
            throw new EmptyException("顺序表位空！");
        }
    }
    private boolean isEmpty() {
        return this.size == 0;
    }
    @Override
    public void set(int pos, int value) {
        try {
            checkEmpty();
            checkPos1(pos);
            this.array[pos] = value;
        }catch (PosIllegal e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void remove(int toRemove) {
        try{
            checkEmpty();
            int pos = indexOf(toRemove);
            if(pos == -1) {
                System.out.println("顺序表中没有你要删除的元素！");
                return;
            }
            for(int i = pos;i < this.size - 1;i++) {
                array[i] = array[i + 1];
            }
            this.size--;

        }catch (EmptyException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public void clear() {
        this.size = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.println(this.array[i] + " ");
        }
    }
}
